import sort.Sort;

import java.util.Arrays;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-07-26
 * Time: 13:39
 */
public class Test {
    public static void orderArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = i;
            /*//逆序
            array[i] = array.length-i;*/
        }
    }

    //随机
    public static void notOrderArray(int[] array) {
        Random random = new Random();

        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(10_0000);
        }
    }



    /**
     * 测试算法是否正确
     * @param args
     */
    public static void testSimple() {
        int[] array = new int[]{12,5,2,9,10,7,2,21,34,19};
        System.out.print("排序前 " + Arrays.toString(array));
        System.out.println();

       /* Sort.insetSort(array);
        System.out.print("插入排序后 " + Arrays.toString(array));
        System.out.println();

        Sort.shellSort(array);
        System.out.print("希尔排序后 " + Arrays.toString(array));
        System.out.println();

        Sort.selectSort(array);
        System.out.print("选择排序后 " + Arrays.toString(array));
        System.out.println();

        Sort.selectSort1(array);
        System.out.print("选择排序后 " + Arrays.toString(array));
        System.out.println();

        Sort.heapSort(array);
        System.out.print("堆排序后 " + Arrays.toString(array));
        System.out.println();

        Sort.quickSort(array);
        System.out.print("快排序后 " + Arrays.toString(array));
        System.out.println();

        Sort.quickSortNor(array);
        System.out.print("非递归快排序后 " + Arrays.toString(array));
        System.out.println();

        Sort.mergeSort(array);
        System.out.print("归并排序后 " + Arrays.toString(array));
        System.out.println();*/

        /*Sort.mergeSortNor(array);
        System.out.print("快排序后 " + Arrays.toString(array));
        System.out.println();*/

        Sort.countSort(array);
        System.out.print("计数排序后 " + Arrays.toString(array));
        System.out.println();

    }

    public static void testOther() {
        int[] array = new int[10_0000];
        orderArray(array);//初始化
//        notOrderArray(array);

        testInsert(array);
        testShellSort(array);
        testSelectSort(array);
        testHeapSort(array);
        testQuickSort(array);
        testMergeSort(array);

    }

    public static void testInsert(int[] array) {
        //这里排序前可以拷贝一个数组来排序，就不影响测试下一个排序了
        array = Arrays.copyOf(array,array.length);

        long startTime = System.currentTimeMillis();
        Sort.insetSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("插入排序耗时" + (endTime-startTime));

    }

    public static void testShellSort(int[] array) {
        //这里排序前可以拷贝一个数组来排序，就不会破坏原来的数组
        array = Arrays.copyOf(array,array.length);

        long startTime = System.currentTimeMillis();
        Sort.shellSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("希尔排序耗时" + (endTime-startTime));

    }

    public static void testSelectSort(int[] array) {
        //这里排序前可以拷贝一个数组来排序，就不会破坏原来的数组
        array = Arrays.copyOf(array,array.length);

        long startTime = System.currentTimeMillis();
        Sort.selectSort1(array);
        long endTime = System.currentTimeMillis();
        System.out.println("选择排序耗时" + (endTime-startTime));

    }

    public static void testHeapSort(int[] array) {
        //这里排序前可以拷贝一个数组来排序，就不会破坏原来的数组
        array = Arrays.copyOf(array,array.length);

        long startTime = System.currentTimeMillis();
        Sort.heapSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("堆排序耗时" + (endTime-startTime));

    }

    public static void testQuickSort(int[] array) {
        //这里排序前可以拷贝一个数组来排序，就不会破坏原来的数组
        array = Arrays.copyOf(array,array.length);

        long startTime = System.currentTimeMillis();
        Sort.quickSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("快排序耗时" + (endTime-startTime));

    }

    public static void testMergeSort(int[] array) {
        //这里排序前可以拷贝一个数组来排序，就不会破坏原来的数组
        array = Arrays.copyOf(array,array.length);

        long startTime = System.currentTimeMillis();
        Sort.mergeSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("归并排序耗时" + (endTime-startTime));

    }

    public static void main(String[] args) {
        System.out.println("====测试算法是否正确====");
        testSimple();

        System.out.println();

        System.out.println("====测试算法速度====");
//        testOther();
    }

}
